var utils = require("./util.js")
var CRYPTO = require("./crypto/crypto.js")
var crypto = new CRYPTO()
var CWRGBSET = require("./opcode.js").CWRGBSET
var CWRGBSETUNACK = require("./opcode.js").CWRGBSETUNACK
var CWRGBGET = require("./opcode.js").CWRGBGET
var ConfigModelSubscriptionAdd = require("./opcode.js").ConfigModelSubscriptionAdd
var ConfigModelSubscriptionDelete = require("./opcode.js").ConfigModelSubscriptionDelete
var getResponseMessage = require("./code.js").getResponseMessage

var modelIds = ["0001005d"];
class LightControl {

  constructor(blemash, bleCommand) {
    this.blemash = blemash;
    this.bleCommand = bleCommand;
  }

  addGroup(groupAddress, address) {
    var configModelSubscriptionAdd = new ConfigModelSubscriptionAdd();
    configModelSubscriptionAdd.ElementAddress = address;
    configModelSubscriptionAdd.Address = groupAddress;
    configModelSubscriptionAdd.ModelIdentifier = modelIds[0];

    configModelSubscriptionAdd.send(this.bleCommand, address)
      .then((op) => {
        this.blemash.emit("addGroup", getResponseMessage(0));
      })
      .catch((error) => {
        this.blemash.emit("addGroup", error);
      }); 
  }
  removeGroup(groupAddress, address) {
    var configModelSubscriptionDelete = new ConfigModelSubscriptionDelete();
    configModelSubscriptionDelete.ElementAddress = address;
    configModelSubscriptionDelete.Address = groupAddress;
    configModelSubscriptionDelete.ModelIdentifier = modelIds[0];

    configModelSubscriptionDelete.send(this.bleCommand, address)
      .then((op) => {
        this.blemash.emit("removeGroup", getResponseMessage(0));
      })
      .catch((error) => {
        this.blemash.emit("removeGroup", error);
      });
  }
  lightControl(address, cold, warm, red, green, blue) {
    var self = this;
    var cwrgbset = new CWRGBSET();
    cwrgbset.setCOLD(cold);
    cwrgbset.setWARN(warm);
    cwrgbset.setRED(red);
    cwrgbset.setGREEN(green);
    cwrgbset.setBLUE(blue);
    cwrgbset.send(this.bleCommand, address)
      .then((op, src) => {
        this.blemash.emit("lightControl", getResponseMessage(0, op));
      })
      .catch((error) => {
        this.blemash.emit("lightControl", error);
      });
  }
  groupControl(groupAddress, cold, warm, red, green, blue) {
    var cwrgbsetunack = new CWRGBSETUNACK();
    cwrgbsetunack.setCOLD(cold);
    cwrgbsetunack.setWARN(warm);
    cwrgbsetunack.setRED(red);
    cwrgbsetunack.setGREEN(green);
    cwrgbsetunack.setBLUE(blue);
    cwrgbsetunack.send(this.bleCommand, groupAddress);
  }
  closeLight(address) {
    let cwrgbset = new CWRGBSET();
    cwrgbset.setRED("00");
    cwrgbset.setGREEN("00");
    cwrgbset.setBLUE("00");
    cwrgbset.setCOLD("00");
    cwrgbset.setWARN("00");
    cwrgbset.send(this.bleCommand, address)
      .then((op, src) => {
        this.blemash.emit("closeLight", getResponseMessage(0));
      })
      .catch((error) => {
        this.blemash.emit("closeLight", error);
      });
  }
  groupCloseLight(groupAddress) {
    var cwrgbsetunack = new CWRGBSETUNACK();
    cwrgbsetunack.setRED("00");
    cwrgbsetunack.setGREEN("00");
    cwrgbsetunack.setBLUE("00");
    cwrgbsetunack.setCOLD("00");
    cwrgbsetunack.setWARN("00");
    cwrgbsetunack.send(this.bleCommand, groupAddress);
  }
  allCloseLight() {
    var cwrgbsetunack = new CWRGBSETUNACK();
    cwrgbsetunack.setRED("00");
    cwrgbsetunack.setGREEN("00");
    cwrgbsetunack.setBLUE("00");
    cwrgbsetunack.setCOLD("00");
    cwrgbsetunack.setWARN("00");
    cwrgbsetunack.send(this.bleCommand, "ffff");
  }
  getMeshStatus() {
    let cwrgbget = new CWRGBGET();
    cwrgbget.send(this.bleCommand, "ffff")
      .success((op, src) => {
        this.blemash.emit("meshStatus", src, op);
      });
  }

}


module.exports = LightControl;